#include <iostream>
#include <fstream>
#include "Sales_data.h"

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        std::cerr << "Please enter file name: " << std::endl;
        return -1;
    }
    std::ifstream in(argv[1]);
    if(!in)
    {
        std::cerr << "Can't open input file" << std::endl;
        return -1;
    }
    std::ofstream out(argv[2], std::ofstream::app);
    if(!out)
    {
        std::cerr << "Cann't open output file." << std::endl;
        return -1;
    }

    Sales_data total;
    if(read(in, total))
    {
        Sales_data trans;
        while(read(in, trans))
        {
            if(total.isbn() == trans.isbn())
            {
                total.combine(trans);
            }
            else
            {
                print(std::cout, total) << std::endl;
                total = trans;
            }
            print(std::cout, total) << std::endl;
        }
    }
    else
    {
        std::cerr << "No data." << std::endl;
    }
    return 0;
}